package com.tang.subject.domain.config;


import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 自定义线程工厂
 *
 * @author Tang
 * @date 2024/2/20 10:03
 **/
public class CustomNameThreadFactory implements ThreadFactory {

    private ThreadFactory threadFactory = Executors.defaultThreadFactory();

    private String namePrefix;

    private AtomicInteger number = new AtomicInteger(0);

    public CustomNameThreadFactory(String namePrefix) {
        this.namePrefix = namePrefix;
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread thread = threadFactory.newThread(r);
        thread.setName(namePrefix + "-" + number.getAndIncrement() + "-thread-");
        return thread;
    }
}
